package com.itheima.mall.web.sms;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.pojo.sms.SmsFlashPromotionSession;
import com.itheima.mall.service.sms.FlashPromotionSessionService;
import com.itheima.mall.service.sms.SmsFlashPromotionService;
import com.itheima.mall.vo.R;
import com.itheima.mall.vo.SessionVo;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/flashSession")
public class FlashPromotionSessionController {
  @Autowired
    FlashPromotionSessionService sessionService;
    @PostMapping("create")
  public R<String> createSession(@RequestBody Map map){

        System.out.println(map);

     String startTime = (String) map.get("startTime");
        String endTime = (String) map.get("endTime");
        DateTime STime = DateUtil.parseDate(startTime);
        System.out.println(STime);
        DateTime Etime = DateUtil.parseDate(endTime);
        boolean before = STime.before(Etime);
        if(before){
            throw new BusinessException("开始时间晚于结束时间");
        }

        SmsFlashPromotionSession session=new SmsFlashPromotionSession();
        session.setStatus(0);
        session.setCreateTime(LocalDateTime.now());
        session.setEndTime(LocalTime.now());
        session.setStartTime(LocalTime.now());
        session.setName(LocalTime.now()+"");
        sessionService.createSession(session);

        return R.succeed(null,"添加成功");
    }
    @PostMapping("delete/{id}")
    public R<String> deleteSession(@PathVariable Integer id){
        sessionService.deleteSession(id);
        return R.succeed(null,"删除成功");
    }
    @GetMapping("list")
    public R<List<SmsFlashPromotionSession>> getList(){
        R<List<SmsFlashPromotionSession>> list = sessionService.getList();
        return list;
    }
    @PostMapping("update/status/{id}")
    public R<String> updateStatus(@PathVariable Integer id,@RequestParam("status") Integer status){
        sessionService.updateStatus(id,status);
        return R.succeed(null,"更新成功");
    }
    @PostMapping("/update/{id}")
      public R<String> updateSession(@RequestBody Map map ,@PathVariable Integer id) {
        SmsFlashPromotionSession session=new SmsFlashPromotionSession();
        session.setStatus(0);
        session.setCreateTime(LocalDateTime.now());
        session.setEndTime(LocalTime.now());
        session.setStartTime(LocalTime.now());
        session.setName(LocalTime.now()+"");
        sessionService.updateSession(session);
        return R.succeed(null,"修改成功");
    }
    @GetMapping("{id}")
    public R<SmsFlashPromotionSession> getSession(@PathVariable Integer id){
        SmsFlashPromotionSession session = sessionService.getSession(id);
        return R.succeed(session,"查询成功");
    }
    @GetMapping("selectList")
    public R<List<SessionVo>> selectList(@RequestParam("flashPromotionId") Integer flashPromotionId ){
        List<SessionVo> list = sessionService.selectList(flashPromotionId);
        return R.succeed(list,"查询成功");
    }
}
